如何应用 actionButton 在 R 中的 Shiny 中更新我的 ggplot?
How to apply the actionButton to update my ggplot in Shiny in R?
这是我的可重现示例:
#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
里放什么?
在reproducible example的第20行,变量w
和b
是我希望在闪亮的server
.
中控制的值
我已经尝试使用 sliderInput
但在这里我有 2 个变量(w
和 b
)?
有没有更好的展示我的剧本?由于我对闪亮很陌生,我希望我能从你们任何人那里得到一些guide/hints..
请检查下方。我已将数据生成放在开头 #global
下,因为这只需要 运行 一次。然后我添加了 reactiveValues
和一个 observeEvent
,这是使用 actionButton
所需的主要内容。参见 Using Action Buttons。使用 reactiveValues
以便绘图在启动时显示,并且最初不需要 actionButton
。如果 w
或 b
已更改,即使您单击 actionButton
,它也只会重新 运行 代码。我已经注释掉了所有不必要的代码以供自己测试。
library(shiny)
library(neuralnet)
require(ggplot2)
# global
traininginput <- as.data.frame(runif(50, min=0, max=100))
trainingoutput <- sqrt(traininginput)
trainingdata <- cbind(traininginput,trainingoutput)
colnames(trainingdata) <- c("Input","Output")
testdata <- as.data.frame((1:13)^2) #Generate some squared numbers
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, session) {
values <- reactiveValues(
w = 5,
b = 5
)
observeEvent(input$update, {
values$w <- input$w
values$b <- input$b
})
output$main_plot <- renderPlot({
net.sqrt <- neuralnet(Output~Input,trainingdata, hidden=c(values$w, values$b), threshold=0.01)
#print(net.sqrt)
#plot(net.sqrt)
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)
这是我的可重现示例:
#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
里放什么?
在reproducible example的第20行,变量w
和b
是我希望在闪亮的server
.
我已经尝试使用 sliderInput
但在这里我有 2 个变量(w
和 b
)?
有没有更好的展示我的剧本?由于我对闪亮很陌生,我希望我能从你们任何人那里得到一些guide/hints..
请检查下方。我已将数据生成放在开头 #global
下,因为这只需要 运行 一次。然后我添加了 reactiveValues
和一个 observeEvent
,这是使用 actionButton
所需的主要内容。参见 Using Action Buttons。使用 reactiveValues
以便绘图在启动时显示,并且最初不需要 actionButton
。如果 w
或 b
已更改,即使您单击 actionButton
,它也只会重新 运行 代码。我已经注释掉了所有不必要的代码以供自己测试。
library(shiny)
library(neuralnet)
require(ggplot2)
# global
traininginput <- as.data.frame(runif(50, min=0, max=100))
trainingoutput <- sqrt(traininginput)
trainingdata <- cbind(traininginput,trainingoutput)
colnames(trainingdata) <- c("Input","Output")
testdata <- as.data.frame((1:13)^2) #Generate some squared numbers
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, session) {
values <- reactiveValues(
w = 5,
b = 5
)
observeEvent(input$update, {
values$w <- input$w
values$b <- input$b
})
output$main_plot <- renderPlot({
net.sqrt <- neuralnet(Output~Input,trainingdata, hidden=c(values$w, values$b), threshold=0.01)
#print(net.sqrt)
#plot(net.sqrt)
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)