【问题标题】:How to apply the actionButton to update my Shiny in R?如何应用 actionButton 来更新我在 R 中的 Shiny?
【发布时间】:2020-07-31 03:27:46
【问题描述】:

这是我的可重现示例:

#http://gekkoquant.com/2012/05/26/neural-networks-with-r-simple-example/

library("neuralnet")
require(ggplot2)
setwd(dirname(rstudioapi::getSourceEditorContext()$path))

#Going to create a neural network to perform sqare rooting
#Type ?neuralnet for more information on the neuralnet library

#Generate 50 random numbers uniformly distributed between 0 and 100
#And store them as a dataframe
traininginput <-  as.data.frame(runif(50, min=0, max=100))
trainingoutput <- sqrt(traininginput)

#Column bind the data into one variable
trainingdata <- cbind(traininginput,trainingoutput)
colnames(trainingdata) <- c("Input","Output")

#Train the neural network
net.sqrt <- neuralnet(Output~Input,trainingdata, hidden=c(input$w, input$b), threshold=0.01)
print(net.sqrt)

#Plot the neural network
plot(net.sqrt)

#Test the neural network on some test data
testdata <- as.data.frame((1:13)^2)  #Generate some squared numbers
net.results <- predict(net.sqrt, testdata) #Run them through the neural network

#Lets see what properties net.sqrt has
class(net.results)

#Lets see the results
print(net.results)

#Lets display a better version of the results
cleanoutput <- cbind(testdata,sqrt(testdata),
                     as.data.frame(net.results))
colnames(cleanoutput) <- c("Input","ExpectedOutput","NeuralNetOutput")
head(cleanoutput)
lm1<- lm(NeuralNetOutput~ ExpectedOutput, data = cleanoutput)
ggplot(data = cleanoutput, aes(x= ExpectedOutput, y= NeuralNetOutput)) + geom_point() +
  geom_abline(intercept = 0, slope = 1
              , color="brown", size=0.5)

这是我在shiny 中尝试过的代码:

library(shiny)
library("neuralnet")
require(ggplot2)

ui <- fluidPage(
  fluidRow(
    column(width = 12, class = "well",
           h4("Neural Network Plot"),

           plotOutput("main_plot"),

           hr(),

           numericInput(inputId = "w",
                       label = "Weight(w):",
                       value = 5),

           numericInput(inputId = "b",
                       label = "Biased(b):",
                       value = 5), 

           actionButton("update", "Update View"))))
#--------------------------------------------------------------------------------------------
server <- function(input, output) {

  output$main_plot <- renderPlot({
    traininginput <-  as.data.frame(runif(50, min=0, max=100))
    trainingoutput <- sqrt(traininginput)
    trainingdata <- cbind(traininginput,trainingoutput)
    colnames(trainingdata) <- c("Input","Output")
    net.sqrt <- neuralnet(Output~Input,trainingdata, hidden=c(input$w, input$b), threshold=0.01)
    print(net.sqrt)
    plot(net.sqrt)
    testdata <- as.data.frame((1:13)^2)  #Generate some squared numbers
    net.results <- predict(net.sqrt, testdata) #Run them through the neural network
    class(net.results)
    print(net.results)
    cleanoutput <- cbind(testdata,sqrt(testdata),
                         as.data.frame(net.results))
    colnames(cleanoutput) <- c("Input","ExpectedOutput","NeuralNetOutput")
    head(cleanoutput)
    lm1<- lm(NeuralNetOutput~ ExpectedOutput, data = cleanoutput)

    ggplot(data = cleanoutput, aes(x= ExpectedOutput, y= NeuralNetOutput)) + geom_point() +
      geom_abline(intercept = 0, slope = 1
                  , color="brown", size=0.5)})}

shinyApp(ui,server)

我希望添加一个真正有效的actionButton,以便我可以更新我的视图而不是让它自动更新。我应该在server.R 里面放什么?

在可重现示例的第 20 行中,变量 wb 是我希望在 shiny 服务器中控制的值。

还有没有更好的方法来展示我的脚本?由于我对闪亮还是很陌生,我希望我能从你们中的任何人那里得到一些小指南/提示..

【问题讨论】:

    标签: r shiny action-button


    【解决方案1】:

    查看actionButton 的文档示例,特别是isolate 在那里的使用方式。这是后代示例中的代码:

    ui <- fluidPage(
      sliderInput("obs", "Number of observations", 0, 1000, 500),
      actionButton("goButton", "Go!"),
      plotOutput("distPlot")
    )
    
    server <- function(input, output) {
      output$distPlot <- renderPlot({
        # Take a dependency on input$goButton. This will run once initially,
        # because the value changes from NULL to 0.
        input$goButton
    
        # Use isolate() to avoid dependency on input$obs
        dist <- isolate(rnorm(input$obs))
        hist(dist)
      })
    }
    

    通常,当您移动滑块时,闪亮会“连续”更新直方图。当您隔离此变量时,它会等待按下按钮。

    【讨论】:

    • 嗨,对于你给我的答案,滑块只有 1 个变量。但是,如果我有 2 个变量,我该怎么办?如果我没有说清楚,我很抱歉。我的意思是我的变量同时是“w”和“b”......
    • 如果它显示“错误:data 必须是一个数据框,或者由fortify() 强制转换的其他对象,而不是具有类 integer/shinyActionButtonValue 的 S3 对象”会发生什么?提前谢谢你
    • @Gambit 请展示您正在尝试做的最小可复制示例。
    猜你喜欢
    • 2020-08-03
    • 1970-01-01
    • 2020-07-27
    • 2021-04-05
    • 2021-02-19
    • 2021-10-05
    • 2018-06-11
    • 2020-07-07
    • 2022-01-02
    相关资源
    最近更新 更多