shiny interactive plot outputs the real coordinates on the data? - r

My app that i built outputs the coordinates of the clicked point but my objective is to output the coordinates of the plotted point and that is by printing out the real correspendent coordinates existing on the x axis .This is the first part of the post i want to resolve, then if it is done then we will deal with the fact that my app deals with an x axis of a date nature then we will plot another plot, instaed of the actual, which in fact based on filtering the data based on that date .But now i want to get using the code below the abscisse which is written on the x axis and not that numerical delivered but R shiny:
library(ggplot2)
library(shiny)
library(shiny)
df<-data.frame("pr"=c("a","n","z","o"),"value"=c(5,1,13,9))
ui <- basicPage(
plotOutput("plot1", click = "plot_click"),
verbatimTextOutput("info")
)
server <- function(input, output) {
output$plot1 <- renderPlot({
plot(as,factor(df$pr ), df$value)
})
output$info <- renderText({
paste0("x=", input$plot_click$x, "\ny=", input$plot_click$y)
})
}
shinyApp(ui, server)
By the way who can disply the points in form of points and not in form of bars or bold lines like in my example so please do it .i will be thankful.

Related

Create Shiny App with histogram and slidebar and save values

I'm trying to create a histogram of distances between cells with slidebar that varies the range of the x-axis like this:
ui <- fluidPage(
# Application title
titlePanel("Distribution of distance between nucleus"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
sliderInput("x_range", "Range:",
min = min(histo$Dist_Closest_Cell),
max = max(histo$Dist_Closest_Cell),
value = c(min(histo$Dist_Closest_Cell),
max(histo$Dist_Closest_Cell)))
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot")
)
)
)
server <- function(input, output) {
output$distPlot <- renderPlot({
# draw the histogram with the specified range
hist(histo$Dist_Closest_Cell,
xlim = c(input$x_range[1], input$x_range[2]),
breaks = 100,
xlab = "Distance µm",
main = "Distribution of distance between nucleus")
})
}
# Run the application
shinyApp(ui = ui, server = server)
histo$Dist_Closest_Cell
I have a DataFrame called histo that has some columns and one of them is Dist_Closest_Cell that contains distance values.
Now, I want to save in a variable as DataFrame the info of histo but in the x-axis range selected with the slidebar. This Shiny App (the code above) is interjected in the middle of a bigger code, so after run the app and choosing the x-axis range I would like to stop the app and return the mencioned DataFrame variable.
I know that with runApp() and stopApp() I could continue with the rest of the code but I don´t know how to write it properly...
Maybe with something like:
...
# Previous code
df <- runApp(
# Above code
return(c(input$x_range[1], input$x_range[2]))
stopApp()
)
# Next code
...
I´m trying this with Shiny because it's the only way to plot a dinamic histogram with slidebar that I've found but if you know another alternative it's welcome.
Thank you in advance.

How to drag a plot line and back into the resulting line parameters in R Shiny?

Here's a long-shot question. The below code allows the user to build and alter a scaled-logarithmic curve by altering its 4 parameters, via slider inputs. I'd like to reverse the process, so the user clicks/drags the plot line and a new "exponential" curve parameter is backed into. How to do this in R Shiny?
Later, after figuring out how to derive the exponential parameter, I'll try backing into some of the other curve parameters too.
This image illustrates what I'm trying to do:
Code:
library(shiny)
ui <- fluidPage(
sliderInput('periods','Nbr of periods:',min=0,max=36,value=24),
sliderInput('start','Start value:',min=0,max=1,value=0.15),
sliderInput('end','End value:',min=0,max=1,value=0.70),
sliderInput('exponential','Exponential:',min=-100,max=100,value=10),
plotOutput('plot')
)
server <- function(input, output, session) {
data <- reactive({
data.frame(
Periods = c(0:input$periods),
ScaledLog = c(
(input$start-input$end) *
(exp(-input$exponential/100*(0:input$periods))-
exp(-input$exponential/100*input$periods)*(0:input$periods)/input$periods)) +
input$end
)
})
output$plot <- renderPlot(plot(data(),type='l',col='blue',lwd=5))
}
shinyApp(ui,server)

the ggplot won't display in the shiny [duplicate]

I'm trying to implement these 3 lines of code, which is my desired output, in Shiny.
install.packages("ggalt")
library(ggalt)
health <- read.csv("https://raw.githubusercontent.com/selva86/datasets/master/health.csv")
health$Area <- factor(health$Area, levels=as.character(health$Area)) # did something for graph
ggplot(health, aes(x=pct_2013, xend=pct_2014, y=Area))+
geom_dumbbell()
In Shiny, I want a user to be able to choose starting and ending points of a lollipop graph. I also want a user to be able to choose a categorical variable (among many categorical variables -- even though for now, the example data only has one categorical variable) that would show up on the y-axis. Below is what I worked on so far, literally replacing all variables to input variables (so I think it should work but it doesn't..)
ui<-pageWithSidebar(
headerPanel('Lollipop Chart'),
sidebarPanel(
selectInput('xcol', 'X Variable', names(health), selected=names(health)[1]),
selectInput('val1', 'Starting Value', names(health), selected=names(health)[3]),
selectInput('val2', 'Ending Value', names(health), selected=names(health)[2]) ),
mainPanel(
plotOutput('plot1')
)
)
server<-function(input, output, session) {
health < read.csv("https://raw.githubusercontent.com/selva86/datasets/master/health.csv")
health$Area <- factor(health$Area, levels=as.character(health$Area)) # did something for graph
selectedData <- reactive({
health[, c(input$xcol, input$val1, input$val2)]
})
output$plot1 <- renderPlot({
ggplot(selectedData(), aes(x=input$val1, xend=input$val2, y=input$xcol))+
geom_dumbbell()
})
}
shinyApp(ui, server)
Could anyone help why am I not getting the desired output? :(
The desired plot doesn't show up because you are calling ggplot in a different way. In your shiny app the selected column names are strings:
ggplot(health, aes(x='pct_2013', xend='pct_2014', y='Area'))+
geom_dumbbell()
Replace the aes with aes_string and it will work.
ggplot(health, aes_string(x='pct_2013', xend='pct_2014', y='Area'))+
geom_dumbbell()

Using shiny input values in a ggplot aes

I'm trying to implement these 3 lines of code, which is my desired output, in Shiny.
install.packages("ggalt")
library(ggalt)
health <- read.csv("https://raw.githubusercontent.com/selva86/datasets/master/health.csv")
health$Area <- factor(health$Area, levels=as.character(health$Area)) # did something for graph
ggplot(health, aes(x=pct_2013, xend=pct_2014, y=Area))+
geom_dumbbell()
In Shiny, I want a user to be able to choose starting and ending points of a lollipop graph. I also want a user to be able to choose a categorical variable (among many categorical variables -- even though for now, the example data only has one categorical variable) that would show up on the y-axis. Below is what I worked on so far, literally replacing all variables to input variables (so I think it should work but it doesn't..)
ui<-pageWithSidebar(
headerPanel('Lollipop Chart'),
sidebarPanel(
selectInput('xcol', 'X Variable', names(health), selected=names(health)[1]),
selectInput('val1', 'Starting Value', names(health), selected=names(health)[3]),
selectInput('val2', 'Ending Value', names(health), selected=names(health)[2]) ),
mainPanel(
plotOutput('plot1')
)
)
server<-function(input, output, session) {
health < read.csv("https://raw.githubusercontent.com/selva86/datasets/master/health.csv")
health$Area <- factor(health$Area, levels=as.character(health$Area)) # did something for graph
selectedData <- reactive({
health[, c(input$xcol, input$val1, input$val2)]
})
output$plot1 <- renderPlot({
ggplot(selectedData(), aes(x=input$val1, xend=input$val2, y=input$xcol))+
geom_dumbbell()
})
}
shinyApp(ui, server)
Could anyone help why am I not getting the desired output? :(
The desired plot doesn't show up because you are calling ggplot in a different way. In your shiny app the selected column names are strings:
ggplot(health, aes(x='pct_2013', xend='pct_2014', y='Area'))+
geom_dumbbell()
Replace the aes with aes_string and it will work.
ggplot(health, aes_string(x='pct_2013', xend='pct_2014', y='Area'))+
geom_dumbbell()

Shiny Interactive Graph plot showing row names

I am using Shiny and ggplot2 for an interactive graph. I also used "plot1_click" to get x and y locations.
output$selected <- renderText({
paste0("Value=", input$plot1_click$x, "\n",
"Names=", input$plot1_click$y) }) #how to get names???
This is part of server code. Here what I want is, instead of printing the "y" coordinates, I want to print the corresponding name written in y-axis. Is there any possible way for it??
As far as I know clicking points is not supported in plotOutput. Click events will only return coordinates of the click location. Those coordinates can however be used to figure out the nearest point.
This shiny app from the shiny gallery pages uses the function shiny::nearPoints which does exactly that. Here is a minimal example.
library(shiny)
library(ggplot2)
shinyApp(
fluidPage(
plotOutput("plot", click = "plot_click"),
verbatimTextOutput('print')
),
server = function(input, output, session){
output$plot <- renderPlot({ggplot(mtcars, aes(wt, mpg)) + geom_point()})
output$print = renderPrint({
nearPoints(
mtcars, # the plotting data
input$plot_click, # input variable to get the x/y coordinates from
maxpoints = 1, # only show the single nearest point
threshold = 1000 # basically a search radius. set this big enough
# to show at least one point per click
)
})
}
)
The verbatimTextOutput shows you the nearest point to the clicked location. Notice that nearPoints only works with ggplots like that. But the help page suggests that there is also a way to use this with base graphics.

Resources