In R script, I built a svm model by provide training. and in shiny webpage i designed user interface in which one textbox and two textarea are placed to get input from user.
Now i want that when user submit on button the textbox and textarea value should be used by R script program in which a training is given to model and now new input is used to classify data by built in model.
so how to pass text field value of shiny webpage to R script?
ui <- fluidPage( fluidRow(id="form-row",column(6,offset =3 ,id="form-col",p(id="mandatory","'*' Fields are manadatory"),div(id = "form",
div(id="p-container",p(id="form-header","Your Question")),
div(id="form-field-container",
textInput("title", "*Title", width='100%',placeholder="Your Title is here..."),
# verbatimTextOutput("value"),
textOutput("title_val"),
#for text area width 100%
tags$style(HTML(".shiny-input-container:not(.shiny-input-container-inline){width: 100%;}")),
textAreaInput("description", "*Description", width='100%',rows = 3, resize = "both",placeholder="Description..."),
textOutput("desc_val"),
textAreaInput("code", "*Code", width='100%',rows = 3, resize = "both",placeholder="Write your code..."),
textOutput("code_val"),
useShinyalert(),
actionButton("submit", "Submit", class = "btn-primary")
))
)
)
)
server <- function(input, output, session){
observeEvent( input$submit,{
title_val <- as.character(input$title)
desc_val <- as.character(input$description)
code_val <- as.character(input$code)
ques<- paste(desc_val, code_val,sep=" \n")
shinyalert( title_val, ques, type = "success")
})
}
shinyApp(ui=ui, server=server)
You could do something like this:
eval(parse(text = input$code))
You could use eval
and parse
to evaluate a string.
library(shiny)
library(tidyverse)
library(shinyalert)
ui <- fluidPage( fluidRow(id="form-row",column(6,offset =3 ,id="form-col",p(id="mandatory","'*' Fields are manadatory"),div(id = "form",
div(id="p-container",p(id="form-header","Your Question")),
div(id="form-field-container",
textInput("title", "*Title", width='100%',placeholder="Your Title is here..."),
# verbatimTextOutput("value"),
textOutput("title_val"),
#for text area width 100%
tags$style(HTML(".shiny-input-container:not(.shiny-input-container-inline){width: 100%;}")),
textAreaInput("description", "*Description", width='100%',rows = 3, resize = "both",placeholder="Description..."),
textOutput("desc_val"),
textAreaInput("code", "*Code", width='100%',rows = 3, resize = "both",placeholder="Write your code..."),
textOutput("code_val"),
useShinyalert(),
actionButton("submit", "Submit", class = "btn-primary")
))
)
)
)
server <- function(input, output, session){
model <- reactive({
data <- mtcars # use your own data, might it be in a fileinput or some other data
result <- eval(parse(text = input$code)) # Should include a hint to what way your dataframe is called and how it looks.
result
})
observeEvent( input$submit,{
title_val <- as.character(input$title)
desc_val <- as.character(input$description)
code_val <- as.character(input$code)
ques<- paste(desc_val, code_val,sep=" \n")
shinyalert( title_val, ques, type = "success")
})
}
shinyApp(ui=ui, server=server)
But to be honest, I do not like the idea to parse and evaluate text from a userinput. Far too many possibilites to run into trouble.
The main advantage that someone who is not familiar with R is gone when I have to copy and paste a script as input
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.