2014-10-02 55 views
0

server.R中的input$goButton是由actionButton触发的,应该是在RStudio Shiny中的if语句之内?在闪亮的网页的例子显示:RStudio Shiny actionButton ignored

library(shiny) 
shinyUI(pageWithSidebar(
    headerPanel("Click the button"), 
    sidebarPanel(
    sliderInput("obs", "Number of observations:", 
       min = 0, max = 1000, value = 500), 
    actionButton("goButton", "Go!") 
), 
    mainPanel(
    plotOutput("distPlot") 
) 
)) 

library(shiny) 
shinyServer(function(input, output, message) { 
    output$distPlot <- renderPlot({ 
    # Take a dependency on input$goButton 
    input$goButton 

    # Use isolate() to avoid dependency on input$obs 
    dist <- isolate(rnorm(input$obs)) 
    hist(dist, main=isolate(paste(system(paste("echo", dist[1],"> /tmp/1 && md5sum /tmp/1"),intern=TRUE),collapse=''))) 
    }) 
}) 

我有一个稍微复杂的程序更语句上面的例子中,并在用户点击前的事件发生事件去按钮。这使得我认为input$goButton被忽略时,其中的一个语句是R system()调用。

Shiny Server v1.1.0.10000 
Node.js v0.10.21 
packageVersion: 0.10.0 

任何想法?

+1

这表明依赖是ON语句goButton,因为它在反应表达式内。通过这种方式来看,如果你想依赖你的按钮,只需在你的反应表达式中输入$ goButton,每次点击它时,反应表达式都会被执行。 – 2014-10-02 16:41:16

+0

在我更复杂的例子中,我有一堆'isolate()'调用。这可能是它在goButton之前被触发的原因吗?我可以在被动调用中使用多个'isolate()'吗? – 719016 2014-10-03 08:55:12

+1

提供你的例子,那么我将能够看到什么确切的问题来源 – 2014-10-03 14:36:54

回答

3

我这你想要什么?只要按下按钮,它会增加数+ 1(从0开始),因此有,如果有return()“一无所有”,如果它没有被按下

rm(list = ls()) 
library(shiny) 
runApp(list(ui = pageWithSidebar(
    headerPanel("Click the button"), 
    sidebarPanel(
    sliderInput("obs", "Number of observations:",min = 0, max = 1000, value = 500), 
    actionButton("goButton", "Go!") 
), 
    mainPanel(plotOutput("distPlot"))), 

    server = function(input, output,session) { 
    my_data <- reactive({ 
     if(input$goButton == 0) 
     { 
     return() 
     } 
     isolate({ 
     input$goButton 
     dist <- isolate(rnorm(input$obs)) 
     hist(dist, main=isolate(paste(system(paste("echo", dist[1],"> /tmp/1 && md5sum /tmp/1"),intern=TRUE),collapse=''))) 
     }) 
    }) 
    output$distPlot <- renderPlot({my_data() 
    }) 
    } 
) 
) 
+0

这工作出色。我没有想到我将不得不''输入$ goButton'出现两次,一次在if return()中,另一次在一个大的isolate()中。谢谢。 – 719016 2014-10-04 07:45:08

+0

它不应该在isolate()中。它没有做任何事情。 – graywh 2014-10-24 19:12:59